/**
 * @Description：
 * @author： YouYu 游宇
 * @createTime: 2018-12-05 5:17 PM
 **/

package com.scb.dev.mms.service.impl;


import com.scb.dev.mms.common.DataCommon;
import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.dao.AccountMapper;
import com.scb.dev.mms.feign.AuthorityFeign;
import com.scb.dev.mms.request.RoleVORequest;
import com.scb.dev.mms.service.IRoleService;

import com.scb.dev.mms.vo.RoleVO;
import lombok.extern.slf4j.Slf4j;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: RoleServiceImpl
 * @Description:
 * @author: YouYu 游宇
 * @createTime: 2018-12-05 5:17 PM
 **/
@Slf4j
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private AuthorityFeign authorityFeign;

    @Autowired
    private AccountMapper accountMapper;
    @Override
    public ResponseResult queryAllRole() {
        ResponseResult responseResult= authorityFeign.queryAllRole();
        JSONArray jsonArray= JSONArray.fromObject(responseResult.getData());
        List<RoleVO> roleVOList =  new ArrayList<>();
        for (int i=0;i<jsonArray.size();i++){
            Object o=jsonArray.get(i);
            JSONObject jsonObject=JSONObject.fromObject(o);
            RoleVO roleVO=(RoleVO) JSONObject.toBean(jsonObject, RoleVO.class);
            roleVOList.add(roleVO);
        }
        log.info("roleVoList11 user =="+roleVOList.toString());
        //RoleVO stu=(RoleVO)JSONObject.toBean(jsonObject, Student.class);
        //List<RoleVO> roleVOList =  (List<RoleVO>) responseResult.getData();
        try{
            log.info("isSuccess=="+responseResult.isSuccess());
            if(responseResult.isSuccess()){
                log.info("roleVoList22 user =="+roleVOList.toString());
                for (int i=0;i<roleVOList.size();i++) {
//                    log.info("type of roleVO==="+roleVOList.get(i).getClass().isInstance(RoleVO.class));
                    //RoleVO roleVO=new RoleVO();
                    //roleVO.setRole(roleVOList.get(i).getRole());
                    log.info("creatorAccountName=="+(String)roleVOList.get(i).getRole().getRoleCreatorAccountId());
                    roleVOList.get(i).setRoleCreatorAccountName(accountMapper.selectAccountNameByPrimaryKey(roleVOList.get(i).getRole().getRoleCreatorAccountId()));
                    if(roleVOList.get(i).getRole().getRoleUpdatorAccountId()==null && roleVOList.get(i).getRole().getRoleUpdatorAccountId()==""){
                        roleVOList.get(i).setRoleUpdatorAccountName("暂未更新");
                    }else {
                        roleVOList.get(i).setRoleUpdatorAccountName(accountMapper.selectAccountNameByPrimaryKey(roleVOList.get(i).getRole().getRoleUpdatorAccountId()));
                    }
                    log.info("roleVO=="+roleVOList.get(i));
                }
                responseResult=ResponseResult.responseResult(roleVOList,true,"queryAllRole success!", StatusCode.OPERATIONSUCCESS, DataCommon.ICON_SUCCESS);
            }
        }catch (NullPointerException e){
            log.info("user  NullPointException");
            responseResult=ResponseResult.responseResult(roleVOList,false,"queryAllRole failed! NullPointException!", StatusCode.FINDNULL, DataCommon.ICON_ERROR);
            e.printStackTrace();
        }catch (Exception e){
            log.info("user  Exception");
            responseResult=ResponseResult.responseResult(roleVOList,false,"queryAllRole failed! Unexpected Exception!", StatusCode.EXCEPTIONERROR, DataCommon.ICON_ERROR);
            e.printStackTrace();
        }
        return responseResult;
    }

    @Override
    public ResponseResult queryAllRoleName() {
        return authorityFeign.queryAllRoleName();
    }

    @Override
    public ResponseResult queryRoleNameByRoleId(String roleId) {
        return authorityFeign.queryRoleName(roleId);
    }

    @Override
    public ResponseResult queryMenusByRoleId(String roleId) {
        return authorityFeign.queryMenusByRoleId(roleId);
    }

    @Override
    public ResponseResult addRole(RoleVORequest roleVORequest) {
        //roleVORequest.setRoleCreatorAccountId();
        return authorityFeign.addRole(roleVORequest);
    }

    @Override
    public ResponseResult deleteRole(String roleId) {
        //roleVORequest.setRoleCreatorAccountId();
        return authorityFeign.delete(roleId);
    }

    @Override
    public ResponseResult updateRole(RoleVORequest roleVORequest) {
        return authorityFeign.updateRole(roleVORequest);
    }
}
